package nmap

import (
	"gitee.com/wudicaidou/menciis-sirecog/rules"
	"path"
	"sync"
)

var defaultProbes *ProbeParseResult
var once sync.Once

func GetDefaultProbes() *ProbeParseResult {
	once.Do(func() {
		// 不期望存在错误，所有打包入文件的规则都应该是正确的
		parser := NewProbesParser()
		err := parser.Parse(rules.FSMustByte(false, "/nmap-service-probes.txt"))
		if err != nil {
			panic(err)
		}
		dir, err := rules.FS(false).Open("/")
		if err != nil {
			panic(err)
		}
		fileInfo, err := dir.Readdir(-1)
		if err != nil {
			panic(err)
		}
		for _, info := range fileInfo {
			if info.IsDir() || info.Name() == "nmap-service-probes.txt" {
				continue
			}
			err = parser.Parse(rules.FSMustByte(false, path.Join("/", info.Name())))
			if err != nil {
				panic(err)
			}
		}
		defaultProbes = parser.Result()
		for _, patch := range Patches {
			if err := defaultProbes.ApplyPatch(patch); err != nil {
				panic(err)
			}
		}
	})
	return defaultProbes
}
